//
//
//import java.util.concurrent.*;
//import java.util.concurrent.atomic.AtomicReference;
//
//public class CompletableFutureWithTimeout {
//
//    private static final ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
//
//    public static void main(String[] args) throws InterruptedException {
//        CompletableFuture<String> future = createLongRunningFuture();
//
//        AtomicReference<Throwable> throwableRef = new AtomicReference<>();
//
////        CompletableFuture<String> resultFuture = future
////                .exceptionally(throwable -> {
////                    throwableRef.set(throwable);
////                    return null;
////                })
////                .("Timed out", 5, TimeUnit.SECONDS);
//
//        System.out.println("Result: " + resultFuture.join());
//
//        Throwable throwable = throwableRef.get();
//        if (throwable instanceof TimeoutException) {
//            System.out.println("Operation timed out.");
//        } else if (throwable != null) {
//            throwable.printStackTrace();
//        }
//    }
//
//    private static CompletableFuture<String> createLongRunningFuture() {
//        CompletableFuture<String> future = new CompletableFuture<>();
//        scheduledExecutor.schedule(() -> {
//            try {
//                Thread.sleep(10000); // Simulate a long running operation
//                future.complete("Completed after 10 seconds");
//            } catch (InterruptedException e) {
//                future.completeExceptionally(e);
//            }
//        }, 0, TimeUnit.MILLISECONDS);
//        return future;
//    }
//}
